﻿/*!	@file	r_skeleton_score.h
	@brief	テストスコア
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "skeleton_score.h"

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../src/lib/rtti/r_rtti_reflection_implement.h"
RRTTI_REFLECTION_IMPLEMENT ( cSkeletonScore, cRScoreClient )
// <<<<<<<<<< RTTI <<<<<<<<<<

static const r_si32		sSphereX = 12;
static const r_si32		sSphereY = 12;
static const r_si32		sSphereVCount = sSphereX * sSphereY;
static const r_si32		sSphereICount = sSphereX * sSphereY * 6;

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// テストコード
#include "lib/draw/r_draw_core.h"
#include "lib/draw/r_draw_context.h"
#include "lib/draw/r_draw_inputlayout.h"
#include "lib/draw/r_draw_inputlayout_desc.h"
#include "lib/draw/r_draw_sampler.h"
#include "lib/draw/r_draw_texture2d.h"
#include "lib/draw/r_draw_vertexShader.h"
#include "lib/draw/r_draw_pixelShader.h"
#include "lib/draw/r_draw_vertexbuffer.h"
#include "lib/draw/r_draw_indexbuffer.h"
#include "lib/draw/r_draw_constantbuffer.h"
#include "lib/draw/r_draw_rasterizerState.h"
#include "lib/draw/r_draw_depthStencilState.h"
#include "lib/draw/r_draw_depthStencil.h"
#include "lib/draw/r_draw_renderTarget.h"
#include "lib/stream/r_stream_default.h"
#include "lib/math/r_math_colorRGBf.h"
#include "lib/math/r_math_def.h"

#include "lib/render/r_render_obj_sphere.h"

#include "lib/window/r_window_systeminfo.h"

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#if 1
// テスト
#include "lib/gui/r_gui_widget_window.h"
#include "lib/gui/r_gui_widget_statictext.h"
#include "lib/gui/r_gui_widget_button.h"
#include "lib/gui/r_gui_widget_textedit.h"
#include "lib/gui/r_gui_widget_intset.h"
#include "lib/gui/r_gui_widget_floatset.h"
#include "lib/gui/r_gui_window_debuginfo.h"
#include "lib/gui/r_gui_server.h"
#include "lib/gui/r_gui_widget_texture_view.h"

#include "lib/develop/r_develop_command.h"
#endif

#include "lib/raytrace/r_raytrace.h"

#include "lib/resource/r_resource_server.h"
#include "lib/resource/texture/r_resource_texture.h"

// ----------------------------------

cSkeletonScore :: cSkeletonScore()
#if 0
: sphereVB( NULL )
, sphereIB( NULL )
, plane( NULL )
, screen( NULL )
#endif
: sphereData( NULL )
{
}

cSkeletonScore :: ~cSkeletonScore()
{
}

void	cSkeletonScore :: init()
{

	r::gui::Server::instance	gui;
#if 0
	WidgetWindow*		guiwindow		= new WidgetWindow();
	guiwindow->setName(_RST("window"));
	gui->addChild(guiwindow);
	WidgetStaticText*	guitext			= new WidgetStaticText();
	guitext->setName(_RST("this is child"));
	guiwindow->addChild(guitext);
	WidgetButton*	guibutton			= new WidgetButton();
	guibutton->setName(_RST("button"));
	guibutton->setSize(196, 12);
	guiwindow->addChild(guibutton);
	guibutton->connectSlotInRelease( signalsBind( &cRDevelopCommand::openSceneGraphWindow, cRDevelopCommand::instance().operator->() ) );

	WidgetTextEdit*	guitexedit		= new WidgetTextEdit();
	guitexedit->setSize(196, 12);
	guiwindow->addChild(guitexedit);

	WidgetIntSet*	guiintedit			= new WidgetIntSet();
	guiwindow->addChild(guiintedit);

	WidgetFloatSet*	guifloatedit	= new WidgetFloatSet();
	guiwindow->addChild(guifloatedit);
#endif
	r::gui::WindowDebugInfo*	guidebug		= new r::gui::WindowDebugInfo();
	gui->addChild(guidebug);

	r::draw::Core::instance	draw;

	r::stream::ReaderDefault tstream ( _RST ( "data\\win32_dx11\\test_shader.hlsl" ) );
	void* tmp = gRAllocDefAllocator[eRAllocDef_Game]->malloc ( tstream.getSize() + 1 );
	tstream.read ( tmp );
	defVS = r::draw::VertexShader::create( tmp, tstream.getSize(), _RST ( "DefVShader" ), _RST ( "test_shader.hlsl" ) );
	defPS = r::draw::PixelShader::create( tmp, tstream.getSize(), _RST ( "DefPShader" ), _RST ( "test_shader.hlsl" ) );
	gRAllocDefAllocator[eRAllocDef_Game]->free ( tmp );

	cRWindowSystemInfo		winInfo;
	r_si32 winW;
	r_si32 winH;
	winInfo.getWindwowSize( winW, winH );
	target = r::draw::RenderTarget::create( winW >> 1, winH >> 1, r::draw::ImageFormat_RGBA8_UN, _RST("test-target") );

	r::draw::RasterizerDesc		rasterdesc;
	rasterdesc.cullMode			= r::draw::CullMode_None;
	raster						= r::draw::RasterizerState::create( rasterdesc );

	r::draw::DepthStencilStateDesc	dsNormalDesc;
	dsNormalDesc.depthFunc					= r::draw::DepthComparisonType_ALWAYS;
	depthStateEqual							= r::draw::DepthStencilState::create( dsNormalDesc );

	t3dmatBuff[0]				= r::draw::ConstantBuffer::create( sizeof ( constantMatrix ) );
	t3dmatBuff[1]				= r::draw::ConstantBuffer::create( sizeof ( constantMatrix ) );
	t3dmatBuff[2]				= r::draw::ConstantBuffer::create( sizeof ( constantMatrix ) );
	t2dmatBuff					= r::draw::ConstantBuffer::create( sizeof ( constantMatrix ) );
	paramBuff					= r::draw::ConstantBuffer::create( sizeof ( constantParams ) );
	r::math::Matrix44	matScreen;
	r::math::Matrix44	matTrans;
	r::math::Matrix44	matCamera;

	matScreen.perspectiveFov_L ( r::math::D2Rad(60.0f), static_cast<r_f32>(winW) / r_f32(winH), 0.1f, 1024.0f );
	matCamera.lookAt_L( r::math::Vector3D( 0, 0, 10 ), r::math::Vector3D( 0, 0, 0 ), r::math::Vector3D( 0, 1, 0 ) );
	matTrans.identity();
	matTrans.setTranslate( r::math::Vector3D(0, 0, 0) );
	matTrans.scale( r::math::Vector3D( 1.0f, 1.0f, 1.0f ) );

	constantMatrix	matData;
	matData.prjMat.identity();
	matData.prjMat = matTrans;
	matData.prjMat *= matCamera;
	matData.prjMat *= matScreen;

	matData.prjMat.transpose();
	t3dmatBuff[0]->map();
	t3dmatBuff[0]->setValue ( &matData );
	t3dmatBuff[0]->unMap();

	matData.prjMat.identity();
	matData.prjMat.setTranslate( r::math::Vector3D(0, 0, 0) );
	matData.prjMat.scale( r::math::Vector3D(5, 5, 5) );
	matData.prjMat = matScreen * matCamera * matData.prjMat;
	matData.prjMat.transpose();
	t3dmatBuff[1]->map();
	t3dmatBuff[1]->setValue ( &matData );
	t3dmatBuff[1]->unMap();

	matData.prjMat.identity();
	matData.prjMat.setTranslate( r::math::Vector3D(0, 0, 0) );
	matData.prjMat.scale( r::math::Vector3D(5, 5, 5) );
	matData.prjMat = matScreen * matCamera * matData.prjMat;
	matData.prjMat.transpose();
	t3dmatBuff[2]->map();
	t3dmatBuff[2]->setValue ( &matData );
	t3dmatBuff[2]->unMap();

	matData.prjMat.identity();
	t2dmatBuff->map();
	t2dmatBuff->setValue ( &matData );
	t2dmatBuff->unMap();

	constantParams params;
	params.clipPlane = r::math::Vector2D( 1.0f, 1024.0f );
	params.sampleDistance = r::math::Vector2D( 1.0f / winW * 0.5f, 1.0f / winH * 0.5f );
	params.loopCount = 4;
	paramBuff->map();
	paramBuff->setValue ( &params );
	paramBuff->unMap();

	sphereData = new r::render::ObjSphere( sSphereX, sSphereY );

	cRDevelopCommand::instance command;
	command->openSceneGraphWindow();

	initRaytrace();
}

r_bool	cSkeletonScore :: checkLoad()
{
	return true;
}

void	cSkeletonScore :: update()
{
	if( !sphereData ){
		return;
	}
	cRWindowSystemInfo		winInfo;
	r_si32 winW;
	r_si32 winH;
	winInfo.getWindwowSize( winW, winH );

	r::draw::Core::instance		draw;
	// テストコード
	r::draw::Context* context = draw->getContext();
	context->OM_SetRenderTargets ( 1, draw->getRenderTargetCore(), draw->getDepthStencilCore() );
	context->ClearRenderTargetView ( draw->getRenderTargetCore(), r::math::ColorRGBf ( 0.5f, 0.5f, 0.8f, 1.0f ) );
	context->ClearDepthStencilView ( draw->getDepthStencilCore(), r::draw::ClearDepthFlag_Depth, 1.0f, 0 );

	context->RS_SetViewPorts ( 1, &r::draw::ViewPortInfo ( 0, 0, static_cast<r_f32>(winW), static_cast<r_f32>(winH), 0.0f, 1.0f ) );
	context->RS_SetState ( raster );
	context->OM_SetDepthStencilState ( depthStateEqual, 0 );
#if 0
	context->IA_SetInputLayout ( objLayout );
#endif

	context->VS_SetShader ( defVS );
	context->PS_SetShader ( defPS );

#if 0
	context->IA_SetIndexBuffer ( sphereIB );
	context->IA_SetVertexBuffer ( 0, sphereVB );
#endif

	context->VS_SetConstantBuffer ( 0, t3dmatBuff[0] );
#if 0
	context->drawIndex ( sSphereICount );

	context->IA_SetIndexBuffer ( boxIB );
	context->IA_SetVertexBuffer ( 0, boxVB );
#endif

	sphereData->draw( context );

	updateRaytrace();
}

void	cSkeletonScore :: destroy()
{
	destroyRaytrace();

	delete sphereData;
	sphereData = NULL;
	r::draw::Core::instance		draw;
}
#if 0
void	cSkeletonScore :: createPlane()
{
	Core::instance		draw;
	plane = VertexBuffer::create( sizeof ( vertexType ), 4 );
	plane->map();
	r_si32 n = 0;
	vertexType tv[] = 
	{
		{ r::math::Vector3D( -1, 0, -1 ), r::math::Vector2D( 0, 0 ) }
		, { r::math::Vector3D( 1, 0, -1 ), r::math::Vector2D( 0, 0 ) }
		, { r::math::Vector3D( -1, 0, 1 ), r::math::Vector2D( 0, 0 ) }
		, { r::math::Vector3D( 1, 0, 1 ), r::math::Vector2D( 0, 0 ) }
	};
	plane->setVertex ( tv );
	plane->unMap();
}

void	cSkeletonScore :: createScreen()
{
	Core::instance		draw;
	screen = VertexBuffer::create( sizeof ( vertexType ), 4 );
	screen->map();
	r_si32 n = 0;
	vertexType tv[] = 
	{
		{ r::math::Vector3D( -1, -1, 0 ), r::math::Vector2D( 0, 0 ) }
		, { r::math::Vector3D( 1, -1, 0 ), r::math::Vector2D( 1, 0 ) }
		, { r::math::Vector3D( -1, 1, 0 ), r::math::Vector2D( 0, 1 ) }
		, { r::math::Vector3D( 1, 1, 0 ), r::math::Vector2D( 1, 1 ) }
	};
	screen->setVertex ( tv );
	screen->unMap();
}
#endif

static r_ui32 tbuffer[1280 * 720];
void	cSkeletonScore :: initRaytrace()
{
	for(r_si32 i = 0; i < 1280*720; i++){
		tbuffer[i] = 0xffffffff;
	}
	raytraceRender		= new r::raytrace::Render();
	raytraceBuffer		= new r::raytrace::Buffer(1280 >> 1,720 >> 1);
	raytraceCamera		= new r::raytrace::Camera();
	raytraceSphere		= new r::raytrace::ObjectSphere();
	raytraceScene		= new r::raytrace::Scene();

	r::draw::Texture2DDesc		texdesc;
	texdesc.mipLevels = 1;
	texdesc.width = 1280 >> 1;
	texdesc.height = 720 >> 1;
	texdesc.format = r::draw::ImageFormat_R8_U;
	texdesc.sampleCount = 1;
	texdesc.sampleQuality = 0;
	texdesc.bindFlags = r::draw::ImageBindFlag_ShaderResource;
	texdesc.usage = r::draw::ImageUsageType_dynamic;
	raytraceResultTexture = r::draw::Texture2D::create( texdesc );

	r::resource::ResourceTexture* texresource = new r::resource::ResourceTexture(raytraceResultTexture);

	r::resource::Server::instance	resource;
	r::resource::ResourcePtr res = resource->asignResource(_RST("raytrace_surface"), texresource);

	r::gui::Server::instance	gui;
	r::gui::WidgetTextureView*	texview		= new r::gui::WidgetTextureView(res);
	gui->addChild(texview);
}

void	cSkeletonScore :: updateRaytrace()
{
//	raytraceRender->beginRender( raytraceScene, raytraceBuffer, *raytraceCamera );
	r::draw::Core::instance draw;
	raytraceResultTexture->texImage(tbuffer, 0);
}

void	cSkeletonScore :: destroyRaytrace()
{
//	raytraceResultTexture->destroy();
	delete raytraceScene;
	delete raytraceRender;
	delete raytraceBuffer;
	delete raytraceCamera;
	delete raytraceSphere;
}

